package service

import (
	"com.cyt.yunpay/app/model"
	"com.cyt.yunpay/app/service/pay"
	"com.cyt.yunpay/library/response"
	"errors"
	"fmt"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
	"github.com/gogf/gf/frame/g"
)

var AllInPayService = new(allinpayService)

type allinpayService struct{}

// 用户注册
func (a *allinpayService) CreateMember(r *ghttp.Request) (interface{}, error) {
	var (
		createMemberReq *model.CreateMemberReq // 创建用户请求验证
		memberEntity    *model.Member
	)

	if err := r.ParseForm(&createMemberReq); err != nil {
		return nil, err
	}

	if err := gconv.Struct(createMemberReq, &memberEntity); err != nil {
		return nil, err
	}

	memberInfo, _ := Member.GetUserByUsername(createMemberReq.Username)
	if memberInfo != nil {
		return nil, errors.New("用户名已存在");
	}

	params := map[string]interface{}{
		"BizUserId":   createMemberReq.Username,
		"MemberType":  createMemberReq.Type,
		"Source":      createMemberReq.Source,
		"ExtendParam": createMemberReq.ExtendParam,
	}
	_, err := pay.CommonPay("allinpay", "MemberCreate", params)
	if err != nil {
		return nil, Errmsg(err)
	} else {
		memberEntity.ChannelId = 1
		memberEntity.Way = 1
		memberEntity.Source = 1
		memberEntity.Status = 1
		memberEntity.Ext = "{}"
		result, err := Member.CreateMember(memberEntity)
		if err != nil {
			return nil, err;
		}
		return result, nil;
	}
}

// 实名认证
func (a *allinpayService) RealNameAuth(r *ghttp.Request) (map[string]interface{}, error) {
	var (
		SetRealNameReq           *model.SetRealNameReq
		memberIdentityInfoEntity *model.MemberIdentityInfo
	)
	if err := r.ParseForm(&SetRealNameReq); err != nil {
		return nil, err
	}

	if err := gconv.Struct(SetRealNameReq, &memberIdentityInfoEntity); err != nil {
		return nil, err
	}

	params := map[string]interface{}{
		"BizUserId":    SetRealNameReq.Username,
		"IsAuth":       SetRealNameReq.IsAuth,
		"Name":         SetRealNameReq.RealName,
		"IdentityType": SetRealNameReq.IdentityType,
		"IdentityNo":   SetRealNameReq.IdentityNo,
	}
	data, err := pay.CommonPay("allinpay", "MemberSetRealName", params)
	if err != nil && gconv.Int(err["errorCode"]) != 30007 {
		return nil, Errmsg(err)
	} else {


		// 保存实名认证信息
		uid := gconv.Int(r.GetCtxVar("uid"))
		tx, err := g.DB().Begin()
		if err != nil {
			return nil, err
		}
		if err := Member.SaveRealNameAuthTx(uid, tx); err != nil {
			tx.Rollback()
			return nil, err
		}
		memberIdentityInfoEntity.Uid = uid
		memberIdentityInfoEntity.Ext = "{}"
		if err := Member.SaveIdentityInfoTx(memberIdentityInfoEntity, tx); err != nil {
			tx.Rollback()
			return nil, err
		}
		tx.Commit()

		//if err := Member.SaveRealNameAuth(uid); err != nil {
		//	return nil, err
		//}
		//
		//memberIdentityInfoEntity.Uid = uid
		//memberIdentityInfoEntity.Ext = "{}"
		//if err := Member.SaveIdentityInfo(memberIdentityInfoEntity); err != nil {
		//	return nil, err
		//}
	}
	return data, nil;
}

// 关联手机
func (a *allinpayService) RelationMobile(r *ghttp.Request) (map[string]interface{}, error) {
	var (
		RelationMobileReq *model.RelationMobileReq
	)
	if err := r.ParseForm(&RelationMobileReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId":        RelationMobileReq.Username,
		"Phone":            RelationMobileReq.Mobile,
		"VerificationCode": RelationMobileReq.Code,
	}

	if RelationMobileReq.Way == 6 { // 解绑
		_, err := pay.CommonPay("allinpay", "MemberUnBindPhone", params)
		if err != nil && gconv.Int(err["errorCode"]) != 30024 {
			return nil, Errmsg(err)
		}
	} else if RelationMobileReq.Way == 9 { // 绑定
		_, err := pay.CommonPay("allinpay", "MemberBindPhone", params)
		if err != nil && gconv.Int(err["errorCode"]) != 30024 {
			return nil, Errmsg(err)
		}
	}

	uid := gconv.Int(r.GetCtxVar("uid"))
	if err := Member.SaveMobile(uid, RelationMobileReq.Mobile); err != nil {
		return nil, err
	}
	return nil, nil;
}

func Errmsg(err map[string]interface{}) error {
	return errors.New(err["message"].(string))
}
